package com.yanqu.road.server.cmd.activity.shuihuyezicard;

import com.yanqu.road.entity.activity.shuihuyezipai.ShuiHuYeZiCardUserBattleInfo;
import com.yanqu.road.pb.activity.ShuiHuYeZiCardProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.manager.activity.shuihuyezipai.ShuiHuYeZiCardActivityBattleMgr;
import com.yanqu.road.server.manager.activity.shuihuyezipai.ShuiHuYeZiCardPVPRoom;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;

/**
 * @Description
 * @Author cwq
 * @Data 2021/12/23 17:44
 */

@Cmd(code = CrossProtocol.C_CROSS_SHUIHUYEZICARD_PVP_SYN_USER_DATA, desc = "水浒叶子牌 玩家登陆 向跨服索要玩家pvp数据")
public class CrossShuiHuYeZiCardSynUserData extends CrossCommand {

    boolean cancelMatch ;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ShuiHuYeZiCardProto.ShuiHuYeZiCardSynUserInfoGameToCrossMsg gameToCrossMsg = ShuiHuYeZiCardProto.ShuiHuYeZiCardSynUserInfoGameToCrossMsg.parseFrom(packet.getBytes());
        cancelMatch = gameToCrossMsg.getCancelMatch();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ShuiHuYeZiCardPVPRoom pvpRoom = ShuiHuYeZiCardActivityBattleMgr.pvpRoomMap.get(packet.getPlayerId());
        if(pvpRoom != null){
            synchronized (pvpRoom){
                // 防止 获取到有正在开启的房间 但是获取完那一刻又结算了。 结算消息先收到，再收到有正在开启的房间
                if(ShuiHuYeZiCardActivityBattleMgr.pvpRoomMap.containsKey(packet.getPlayerId())){
                    ShuiHuYeZiCardUserBattleInfo userBattleInfo = pvpRoom.getTargetUserBattleInfo(packet.getPlayerId());
                    ShuiHuYeZiCardActivityBattleMgr.synUserDataToGameServer(packet.getPlayerId(),serverId,userBattleInfo.getRoomType());
                }
            }
        }else {
            // 尝试取消匹配  因为有可能存 玩家被顶号的情况，区服那边又没有走离线取消匹配。
            if(cancelMatch){
                synchronized (ShuiHuYeZiCardActivityBattleMgr.lock){
                    ShuiHuYeZiCardActivityBattleMgr.waitingPlayerMap.remove(packet.getPlayerId());
                }
            }
        }

    }

    @Override
    public void sendData() {

    }
}
